#include "yocto/utest/run.hpp"

#include "core/frame.hpp"
#include "core/fcc.hpp"

#include "yocto/z++/gzstream.hpp"

using namespace FastMD;

YOCTO_UNIT_TEST_IMPL(frames)
{
	for( size_t iter = 0; iter < 10; ++iter )
	{
		const size_t desired = 1 + LessThan(1000);
		U3D          cr;          //!< fcc crystal data
		V3D          box(5,5,5);  //!< simulation box
		V3D          ibox(box);   //!< reciprocal box
		ibox.reciprocal();        //!< computed
		const size_t np      = FCC::Count( cr, desired, box);
		std::cerr << "desired=" << desired << std::endl;
		std::cerr << "in fcc =" << np << std::endl;
		
		Frame F(1,np);
		FCC::Build( F[0], cr, box, ibox, 0 );
		for( size_t i=0; i < F.particles; ++i )
		{
			const Real mass = 1 + 10 * Rand();
			F[ Frame::POSITIONS  ][i].data = 1/mass;
			F[ Frame::VELOCITIES ][i].data = mass;
		}
		
		F.initialize_velocities(Rand());
		
		if( iter == 0 )
		{
			F[0].save_xyz( "fcc.xyz", "H", "simple fcc", 0 );
		}
		
		//-- direct save
		const string filename = vformat( "frame%u.dat.gz", unsigned(np) );
		F.save( filename, 0, false);

		//-- reload
		Real    t = -1;
		ios::igzstream fp( filename );
		Frame         *pF = Frame::load( fp, t, 3);
		if( pF )
		{
			delete pF;
		}
		
		std::cerr << std::endl;

	}	
	
}
YOCTO_UNIT_TEST_DONE()
